import { Toast, Modal } from 'antd-mobile';

export function isLogin(){
	let local_uid = localStorage.getItem("local_uid_teacher");
	if (local_uid == null || typeof(local_uid) == 'undefined' || local_uid == 0){
		return false;
	}
	return true;
}

export function getLocalUid(){
	let local_uid = 0;
	if (isLogin()){
		local_uid = localStorage.getItem("local_uid_teacher");
	}
	return local_uid;
}

export function getLocalPassword(){
	let local_password = '';
	if (isLogin()){
		local_password = localStorage.getItem("local_password_teacher");
	}
	return local_password;
}

export function getLocalSchoolId(){
	let local_school_id = '';
	if (isLogin()){
		local_school_id = localStorage.getItem("local_school_id");
	}
	return local_school_id;
}

export function doLogin(data){
	localStorage.setItem("local_uid_teacher", data.uid);
	localStorage.setItem("local_password_teacher", data.password);
}

export function doLogout(){
	localStorage.removeItem("local_uid_teacher");
	localStorage.removeItem("local_password_teacher");
}

export function needAuth(nextState, replace){
    if (!isLogin()) {
        replace({ pathname: '/login' }); //未登录时跳转到登录页
    }
}

export function getLocalTime(nS) {
   return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ').replace(/上午|下午/g, "");     
}

export function getLocalTime2(nS) {     
   return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ").substr(0,11);      
}

export function toastLoading(msg = '正在加载...', duration = 600, text = ''){
	if (typeof(api) == 'undefined'){
		Toast.loading(msg, duration);
	}else {
		api.showProgress({
		    style: 'default',
		    animationType: 'fade',
		    title: msg,
		    text: text,
		    modal: true
		});
	}
}

export function toastSuccess(msg, duration = 1, location = 'bottom'){
	if (typeof(api) == 'undefined'){
		Toast.success(msg, duration);
	}else {
		api.toast({
		    msg: msg,
		    duration: duration * 1000,
		    location: location
		});
	}
}

export function toastFail(msg = '服务器错误', duration = 1, location = 'bottom'){
	if (typeof(api) == 'undefined'){
		Toast.fail(msg, duration);
	}else {
		api.toast({
		    msg: msg,
		    duration: duration * 1000,
		    location: location
		});
	}
}

export function toastInfo(msg, duration = 1, location = 'bottom'){
	if (typeof(api) == 'undefined'){
		Toast.info(msg, duration);
	}else {
		api.toast({
		    msg: msg,
		    duration: duration * 1000,
		    location: location
		});
	}
}

export function toastHide(){
	if (typeof(api) == 'undefined'){
		Toast.hide();
	}else {
		api.hideProgress();
	}
}

export function myConfirm(title, msg, cancelText='取消', okText='确定'){
	if (typeof(api) == 'undefined'){
		Modal.alert(title, msg, [
		    { text: cancelText, onPress: () => {return 0;}},
		    { text: okText, onPress: () => {return 1;}},
		  ]);
	}else {
		api.confirm({
		    title: title,
		    msg: msg,
		    buttons: [cancelText, okText]
		}, function(ret, err) {
		    var index = ret.buttonIndex;
		    if (index == 1){ //cancel
		    	return 0;
		    }
		    return 1;
		});
	}

}